package com.github.llyb120.stock.ctrl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import com.github.llyb120.json.Arr;
import com.github.llyb120.json.Json;
import com.github.llyb120.json.Obj;
import com.github.llyb120.namilite.NamiBaseController;
import com.github.llyb120.stock.Ctrl;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import static com.github.llyb120.json.Json.a;
import static com.github.llyb120.json.Json.o;

public class search extends NamiBaseController {


    public Object suggest() {
        String str = FileUtil.readString(new File(Ctrl.dir, "menu.json"), StandardCharsets.UTF_8);
        Obj obj = Json.parse(str);
//        String sql = "select code,name from stock group by code,name";
//        List<Obj> list = sqlManager.execute(sql, Obj.class, o());
        Arr ret = a();
        obj.forEach((k,v) -> {
            ret.add(o("value", k + " " + v));
        });
//        Obj cache = o();
//        for (Obj obj : list) {
//            if (cache.containsKey(obj.ss("code"))) {
//                continue;
//            }
//            ret.add(o("value", obj.ss("code") + " " + obj.ss("name")));
//            cache.put(obj.ss("code"), true);
//        }
        return ret;
    }

    public Object list(){
        CsvData list = CsvUtil.getReader().read(new File(Ctrl.dir, $request.ss("code") + ".csv"), Charset.forName("GBK"));
        List<CsvRow> rows = list.getRows();
        return rows.subList(1, rows.size());
//        String sql = "select low_price,high_price,begin_price,end_price,exchange,trade_count,trade_money,stock_date from stock where code = #code# order by stock_date asc";
//        PageQuery<Obj> pg = new PageQuery<>();
//        pg.setPageNumber($request.ii("page"));
//        pg.setPageSize($request.ii("size"));
//        pg.setParas($request);
//        sqlManager.executePageQuery(sql, Obj.class, pg);
//        return pg;
//        List<Obj> list = sqlManager.execute(sql, Obj.class, $request);
//        sql = "select code,name from stock where code = #code# order by stock_date desc limit 1";
//        return o(
//            "list", list,
//            "info", sqlManager.execute(sql, Obj.class, $request)
//        );

    }

}
